﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Profile;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace WebArchivosTelefonica.Jerarquias
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LogoCliente : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            ProfileBase usuarioProfile = HttpContext.Current.Profile as ProfileBase;
            string tipoUsuario = (string)usuarioProfile.GetPropertyValue("TipoUsuario");

            string idCliente = string.Empty;
            if (tipoUsuario.StartsWith("Cliente_"))
            {
                idCliente = tipoUsuario.Split(new char[1] { '_' })[1];

            }
            if (idCliente != string.Empty)
            {
                DataClassesArbolDataContext dc = new DataClassesArbolDataContext();
                Cliente cliente = dc.Clientes.First(x => x.Id == new Guid(idCliente));
                MemoryStream imagen = null;
                if (cliente != null)
                {
                    if (cliente.Logo != null && cliente.Logo.Length > 0)
                        imagen = new MemoryStream(cliente.Logo.ToArray());
                }

                if (imagen != null)
                {
                    Response.ContentType = "image/bmp";


                    ////Dreclaramos el objeto BitMap y Graphic   
                    Bitmap imagenOriginal = new Bitmap(imagen, true);
                    Bitmap imagenResize = new Bitmap(imagenOriginal, new Size(120, 120));




                    ////Definimos el tipo de fichero   
                    Response.ContentType = "image/jpeg";
                    //Response.ContentType = "image/gif";    
                    ////Y finalmente lo guardamos   


                    imagenResize.Save(Response.OutputStream, ImageFormat.Jpeg);
                }
            }
            else
            {
                string archivo = Server.MapPath("~/Images/logoTesAmerica.jpg");
                Bitmap imagenOriginal = new Bitmap(archivo);
                Bitmap imagenResize = new Bitmap(imagenOriginal, new Size(125, 125));




                ////Definimos el tipo de fichero   
                Response.ContentType = "image/jpeg";
                 
                ////Y finalmente lo guardamos   


                imagenResize.Save(Response.OutputStream, ImageFormat.Jpeg);



            }





        }
        
    }
}
